package com.jinghang.redis;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * description:
 * author: ZhangXu
 * createTime: 2018-08-17 14:11
 **/
public class DateTimeUtil {

    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String TO_DAY_FORMAT = "yyyy-MM-dd";
    public static final String TO_SECOND_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String TO_DAY = "dd";


    public static Date strToDate(String dateTimeStr, String formatStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date, String formatStr) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static Date strToDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

    /**
     * 获取今天日期字符串
     */
    public static String getCurrentDayStr() {
        Calendar a = Calendar.getInstance();
        Date time=a.getTime();
        return dateToStr(time,STANDARD_FORMAT);
    }

    /**
     * 获取今天日期字符串
     */
    public static String getCurrentDayStr(String format) {
        Calendar a = Calendar.getInstance();
        Date time=a.getTime();
        return dateToStr(time,format);
    }

    /**
     * 获取昨天日期
     */
    public static String getYesterdayStr() {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        return dateToStr(time,STANDARD_FORMAT);
    }






    /**
     * 获取当前日
     */
    public static int getCurrentDay() {
        Calendar a = Calendar.getInstance();
        int day = a.get(Calendar.DATE);
        return day;
    }


    /**
     * 获取当前月份
     */
    public static int getCurrentMonth() {
        Calendar a = Calendar.getInstance();
        int month = a.get(Calendar.MONTH) + 1;
        return month;
    }

    /**
     * 获取当前年份
     */
    public static int getCurrentYear() {
        Calendar a = Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        return year;
    }


    /**
     * 获取当月的 天数
     */
    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }



    /**
     *获取当前月份天数
     */
    public static Integer getDayNumOfMunth() {
        Calendar cale;
        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat(TO_DAY);
        String firstday, lastday;
        // 获取前月的第一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());
        // 获取前月的最后一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = format.format(cale.getTime());
        return Integer.valueOf(lastday) - Integer.valueOf(firstday) + 1;
    }

    public static void main(String[] args) {
        //System.out.println(DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        System.out.println(getYesterdayStr());

    }


}


